package cn.zyq.ykb.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.config.ConfigTaskType;
import cn.zyq.ykb.beans.task.Task;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.beans.user.UserBalance;
import cn.zyq.ykb.beans.user.vo.UserWalletDetailVo;
import cn.zyq.ykb.mapper.config.ConfigTaskTypeMapper;
import cn.zyq.ykb.mapper.task.TaskMapper;
import cn.zyq.ykb.mapper.user.UserBalanceMapper;
import cn.zyq.ykb.mapper.user.UserMapper;
import cn.zyq.ykb.service.user.UserBalanceService;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户-余额变更Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-19
 */
@Service
public class UserBalanceServiceImpl extends BaseService implements UserBalanceService {

    @Autowired
    private UserBalanceMapper userbalanceMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private ConfigTaskTypeMapper configTaskTypeMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户-余额变更
     *
     * @param userbalanceID 用户-余额变更主键
     * @return 用户-余额变更
     */
    @Override
    public UserBalance selectUserbalanceByUserBalanceID(Integer userbalanceID) {
        UserBalance userbalance = userbalanceMapper.selectByPrimaryKey(userbalanceID);
        if (userbalance.getDeleted() == 1) {
            return null;
        }
        return userbalance;
    }

    /**
     * 查询用户-余额变更
     *
     * @param serviceID 业务ID
     * @return 用户-余额变更
     */
    @Override
    public UserBalance selectUserbalanceByServiceID(String serviceID) {
        UserBalance param = new UserBalance();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.userbalanceMapper.selectOne(param);
    }

    /**
     * 查询用户-余额变更列表
     *
     * @param pageParam
     * @return 用户-余额变更
     */
    @Override
    public PageInfo<UserWalletDetailVo> selectUserbalanceList(PageParam pageParam, User loginUser) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        pageParam.getKv().put("userID", loginUser.getUserID());
        List<UserBalance> list = this.userbalanceMapper.list(pageParam.getKv());
        ArrayList<UserWalletDetailVo> userWalletDetailVos = new ArrayList<>();
        for (UserBalance userBalance : list) {
            UserWalletDetailVo vos = new UserWalletDetailVo();
            vos.setChangeBalance(userBalance.getChangeBalance());
            vos.setCreateTime(userBalance.getCreateTime());
            vos.setChangeTitle(userBalance.getChangeTitle());
            vos.setCurrentBalance(userBalance.getCurrentBalance());
            vos.setStatus(userBalance.getStatus());
            if (userBalance.getStatus().equals(1) || userBalance.getStatus().equals(2) || userBalance.getStatus().equals(4)){
                Task task = this.taskMapper.selectByPrimaryKey(userBalance.getAssociationID());
                vos.setPicture(task.getLogo());
                User user = this.userMapper.selectByPrimaryKey(userBalance.getSourceUserID());
                if (BeanUtil.isNotEmpty(user)){
                    vos.setSourceUser(user.getNickName());
                }
            }
            userWalletDetailVos.add(vos);
        }
        return new PageInfo<>(userWalletDetailVos);
    }

    /**
     * 查询用户-余额变更列表
     *
     * @param param
     * @return 用户-余额变更
     */
    @Override
    public List<UserBalance> selectUserbalanceList(UserBalance param) {
        param.setDeleted(0);
        return this.userbalanceMapper.select(param);
    }
    /**
     * 新增用户-余额变更
     *
     * @param param 用户-余额变更
     * @return 结果
     */
    @Override
    public int insertUserbalance(UserBalance param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.userbalanceMapper.insertSelective(param);
    }

    /**
     * 修改用户-余额变更
     *
     * @param param 用户-余额变更
     * @return 结果
     */
    @Override
    public int updateUserbalance(UserBalance param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.userbalanceMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除用户-余额变更
     *
     * @param userbalanceIDs 需要删除的用户-余额变更主键
     * @return 结果
     */
    @Override
    public int deleteUserbalanceByUserBalanceIDs(Integer[] userbalanceIDs) {
        int i = 0;
        for (Integer userbalanceID : userbalanceIDs) {
            i += this.deleteUserbalanceByUserBalanceID(userbalanceID);
        }
        return i;
    }

    /**
     * 删除用户-余额变更信息
     *
     * @param userbalanceID 用户-余额变更主键
     * @return 结果
     */
    @Override
    public int deleteUserbalanceByUserBalanceID(Integer userbalanceID) {
        UserBalance userbalance = this.selectUserbalanceByUserBalanceID(userbalanceID);
        if (userbalance == null) {
            return 0;
        } else {
            UserBalance storeInfo = new UserBalance();
            storeInfo.setUserBalanceID(userbalanceID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.userbalanceMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    public PageInfo<UserBalance> txList(PageParam pageParam, User loginUser) {
        List<UserBalance> userBalances = null;
        if (pageParam.getKv().get("state").equals(1)){
            PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
            pageParam.getKv().put("deleted", 0);
            pageParam.getKv().put("userID", loginUser.getUserID());
            userBalances=this.userbalanceMapper.txList(pageParam.getKv());
        }else{
            PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
            pageParam.getKv().put("deleted", 0);
            pageParam.getKv().put("userID", loginUser.getUserID());
            userBalances=this.userbalanceMapper.txListAll(pageParam.getKv());
        }
        for (UserBalance userBalance : userBalances) {
            if (!userBalance.getUserID().equals(0)){
                User user = this.userMapper.selectByPrimaryKey(userBalance.getUserID());
                userBalance.setNickName(user.getNickName());
                userBalance.setAvatar(user.getAvatar());
                userBalance.setPosition(user.getPosition());
            }
        }
        return new PageInfo<>(userBalances);
    }

    @Override
    public Integer txListNum(User loginUser) {
        return this.userbalanceMapper.txListNum(loginUser.getUserID());
    }
}
